<?php

namespace app\api\model\service;

use think\Model;
use addons\service\library\Map;
use think\Exception;
class CityConfig extends Model
{
    // 表名
    protected $name = 'service_city_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function settleTravelPrice($param)
    {
        $cityConfig = self::where(['city'=>$param['city']])->find();
        $addressInfo = \app\api\model\service\Address::where(['id'=>$param['address_id']])->field('lng,lat')->find();
        $skill = Skill::where(['id'=>$param['skill_id'],'state'=>1])->field('lng,lat')->find();
        $distance = Map::countDistance(['user'=>['lng'=>$addressInfo['lng'],'lat'=>$addressInfo['lat']],'skill'=>['lng'=>$skill['lng'],'lat'=>$skill['lat']]]);
        $data['distance'] = $distance;
        $data['traveltype'] = $param['traveltype'];
        //计算出租车费用
        if($param['traveltype'] == 1){
            $data['distance_price'] = $distance<=$cityConfig['init_distance']?sprintf("%.2f",$cityConfig['init_price']*2):sprintf("%.2f",(($distance-$cityConfig['init_distance'])*$cityConfig['add_price']+$cityConfig['init_price'])*2);
        }elseif($param['traveltype'] == 2){
            $data['distance_price'] = 0.00;
        }
        return $data;
    }


}